package org.dong.Dao;

import org.dong.Jdbc.JdbcUtil;
import org.dong.Model.DownloadList;
import org.dong.Model.FileList;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TranDaoImpl extends BeasDao implements TranDao {
    @Override
    public ArrayList<DownloadList> getDownList(Map<String,String> map) {
        Connection conn = JdbcUtil.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        //创建一个集合
        ArrayList<DownloadList> list = new ArrayList<DownloadList>();
        try {
            String sql = "select * from dong_tran_list where user_id = ? ORDER BY id DESC";

            List<Object> data = new ArrayList<>();
            data.add(map.get("id"));
            List<Object> down_list = super.select(sql,data,DownloadList.class);

            if(down_list.size() > 0){
                for (Object o : down_list) {
                    DownloadList down = (DownloadList)o;
//                    System.out.println(down);
                    Long file_size = Long.parseLong(down.getFile_size());

                String bigStr = "";
                //转换大小
                int i;
                for (i = 0; file_size > 1; i++) {


                    if (file_size / 1024 < 1) {
                        break;
                    } else {
                        file_size = file_size / 1024;
                    }

                }
                if (i == 0) {
                    bigStr = "B";
                } else if (i == 1) {
                    bigStr = "KB";
                } else if (i == 2) {
                    bigStr = "MB";
                } else if (i == 3) {
                    bigStr = "GB";
                } else if (i == 4) {
                    bigStr = "TB";
                }


                bigStr = file_size + bigStr;
                down.setFile_size(bigStr);

                list.add(down);
                }
            }

            return list;
        } catch (SQLException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public boolean addDownload(Map<String,String> map) {

        try {

            //查询文件是否存在
            String sql = "select * from dong_file_list where id=? ";
            List<Object> list = new ArrayList<>();
            list.add(map.get("file_id"));
            List<Object> files = super.select(sql,list,FileList.class);

            if(files.size()<=0){
                return false;
            }else{
                //创建新对象
                FileList file = (FileList)files.get(0);

                long timestamp = System.currentTimeMillis(); // 获取当前时间戳
                BigDecimal bigDecimal = new BigDecimal(timestamp);

                sql = "INSERT INTO dong_tran_list( `file_id`,`file_name`, `user_id`, `file_time`, `file_size`,  `type`, `status`) VALUES (?,?,?,?,?,?,0)";

                List<Object> insert = new ArrayList<>();
                insert.add(file.getId());
                insert.add(file.getName());
                insert.add(file.getUser_id());
                insert.add(timestamp);
                insert.add(file.getSize());
                insert.add(map.get("type"));

                if(super.update(sql,insert) > 0){
                    return true;
                }else{
                    return false;
                }
            }



        } catch (SQLException | IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }

        return false;
    }
}
